package com.duomai.myapplication;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;

public class MainAdapter extends BaseAdapter {
    private Context mContext;
    private ArrayList<String> mData;

    public MainAdapter(Context context, ArrayList<String> data) {
        mContext = context;
        mData = data;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        // 在这里返回条目的ID，比如position本身
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.list_item, null); // list_item_layout为每个条目的布局
        }

        // 获取当前位置的数据项
        String item = mData.get(position);

        // 将数据绑定到视图上
        TextView title = view.findViewById(R.id.title); // 假设list_item_layout中有一个TextView
        title.setText("item "+position);
        TextView cont = view.findViewById(R.id.cont); // 假设list_item_layout中有一个TextView
        cont.setText("Sub item "+position);

        return view;
    }
}